var timer;
$(".sel>p").mouseenter(function() {
	$(".sel>ul").hide();
	$(this).parent().children("ul").show();
});
$(".sel>ul").mouseenter(function() {
//	$(this).show();
	clearTimeout(timer);
})
$(".sel>ul").mouseleave(function() {
	$(this).hide();
})
$(".sel>p").mouseleave(function() {
	timer = setTimeout(function() {
		$(".sel>ul").hide();
	}, 100);
})

$("#draggable3").draggable({
	containment: "#sec-img",
	scroll: false,
	cursor: "move",
	cursorAt: {
		top: 115,
		left: 115
	}
});

var imgs = $(".trade-img");
$("#bImg")[0].src = $("#currentImg")[0].src;
imgs.click(function(e) {
	imgs.removeClass("pointer");
	$(e.target).addClass("pointer");
	$("#currentImg")[0].src = $(e.target)[0].src;
	$("#bImg")[0].src = $("#currentImg")[0].src;
})

var dragsize = $("#draggable3").width();
var bigsize = $("#big-img").width();
var nowsize = bigsize / dragsize * bigsize;
//		console.log(dragsize+","+bigsize);

var wid, hei, top1, left1;

$("#sec-img").mousemove(function(e) {
	$("#big-img").show();
	$("#draggable3").show();
	wid = $("#draggable3").position().left;
	hei = $("#draggable3").position().top;
	top1 = hei / bigsize * nowsize;
	left1 = wid / bigsize * nowsize;
	$("#bImg").css({
		"width": nowsize + "px",
		"height": nowsize + "px",
		"position": "absolute",
		"top": -top1 + "px",
		"left": -left1 + "px",
	});
})
$("#sec-img").mouseleave(function() {
	$("#big-img").hide();
	$("#draggable3").hide();
});
$("#add-goods").click(function(){
	$("#tips").show();
})

$("#close").click(function(){
	$("#tips").hide();
})
$("#continue").click(function(){
	location.href = "/";
})

var gData = JSON.parse($.cookie().gData);

var gonum;
if(gonum == undefined){
	gonum=0;
}

if(gData){
	$("#currentImg")[0].src = gData.goodsPic;
	$("#bImg")[0].src = $("#currentImg")[0].src;
	$("#trade-name>div").html(gData.goodsName);
	$(".current-price").html(gData.goodsPrice);
	
	$("#buy-input").blur(function(){
		
		gonum = Math.floor($("#buy-input").val());
		var n = gData.goodsPrice.slice(1);
		
		$("#goods-num").html(gonum);
		$("#all-price").html("￥"+Number(n)*gonum);
		
	})
}




$("#add-goods").click(function(res){
//	console.log(gData.goodsPic)
	var data = {
		userId: $.cookie().userId,
		goodsName: gData.goodsName,
		goodsPrice: gData.goodsPrice,
		goodsPic: gData.goodsPic,
		goodsNum: Number(gonum)
	};
	console.log(data);
//	var a = gData.goodsPic.slice(22,59);
//	var b = a.split("/");
//	console.log(gData.goodsPic)
//	console.log(b);
//	var c = b.urlencode(b[0]);
	
	if(Number(gonum)>=1){
		$.get("/goods/add",data,function(res){
			console.log(res);
			if(res.state==1)
			{
				alert(res.msg);
				return;
			};
			if(res.state==0)
			{
				alert(res.msg);
				return;
			}
		});
	}
});
console.log($.cookie().userId);
$("#search-gouwuche").click(function(){
	
	location.href = "/goods/all?id="+$.cookie().userId;
})
//显示用户名
if($.cookie().username)
{
	$("#div1>p:nth-of-type(2)").html("您好"+$.cookie().username);
	$("p.red:nth-of-type(3)").children("a").html("[个人]");
	$("p.red:nth-of-type(4)").children("a").html("[退出]");
	$("p.red:nth-of-type(4)").children("a").on("click",function()
	{
		$.removeCookie("username");
		location.href = "/";
	});
};

